//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.member;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.ContractSignConfirmDao;
import com.aidong.model.wills.ContractSignConfirm;
import com.aidong.service.PrintDataService;
import com.aidong.web.controller.BaseController;
import java.util.Date;
import java.util.HashMap;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"contractPrint"})
public class ContractPrintController extends BaseController {
    @Resource
    ContractSignConfirmDao contractSignConfirmDao;
    @Resource
    PrintDataService printDataService;

    public ContractPrintController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list(@RequestParam(value = "type",required = false) String type) {
        return (new ModelAndView("member/print/list")).addObject("contractType", this.getTypeList("signContractType")).addObject("type", type);
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "contractType",required = false) String contractType, @RequestParam(value = "contractCode",required = false) String contractCode, @RequestParam(value = "name",required = false) String name, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "shopCode", shopCode);
        cd.eq("printLabel", status);
        cd.eq("contractCode", contractCode);
        cd.eq("contractType", contractType);
        if(!this.chkNull(name)) {
            ConditionMap mv = new ConditionMap(1);
            mv.like("memberCode", "%" + name + "%");
            mv.like("memberName", "%" + name + "%");
            cd.addSub(mv);
        }

        this.contractSignConfirmDao.queryPageList(pg, cd, (new OrderSet()).desc("createTime"));
        ModelAndView mv1 = new ModelAndView("member/print/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"print"})
    public ModelAndView print(@RequestParam("id") int id) {
        ContractSignConfirm signConfirm = (ContractSignConfirm)this.contractSignConfirmDao.getById(Integer.valueOf(id));
        if(signConfirm == null) {
            return this.showAlert("合同号错误!");
        } else {
            StringBuilder msg = new StringBuilder();
            HashMap data = this.printDataService.getContractPrintData(signConfirm.getSaleId().intValue(), signConfirm.getContractType(), msg);
            if(data == null) {
                return this.showAlert(msg.toString());
            } else {
                ModelAndView mv = new ModelAndView("print/print");
                mv.addAllObjects(data);
                mv.addObject("_picPath", this.getSetting("pic.path"));
                return mv;
            }
        }
    }

    @RequestMapping({"_print"})
    @ResponseBody
    public String _print(@RequestParam("saleId") int saleId, @RequestParam("contractType") String contractType) {
        ContractSignConfirm signConfirm = this.contractSignConfirmDao.getBySaleId(saleId, contractType);
        if(signConfirm == null) {
            return this.getResult(false, "合同错误!");
        } else {
            this.addUserLog("打印" + this.cache.getValue("signContractType", signConfirm.getContractType()) + "合同", "合同号:" + signConfirm.getContractCode());
            if(this.chkNullZ(signConfirm.getPrintLabel())) {
                signConfirm.setPrintLabel(Integer.valueOf(1));
                signConfirm.setPrintTime(new Date());
                signConfirm.setPrintUser(this.getLoginUserCode());

                try {
                    this.contractSignConfirmDao.updatePrint(signConfirm);
                } catch (Exception var5) {
                    this.log.error("print contractsign", var5);
                    return this.getResult(false, "操作发生错误");
                }
            }

            return this.getResult(true, "操作成功");
        }
    }
}
